﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using System.Text;

public partial class CustomerArchives_CustomerArchives : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            List<Model.V_CARSET> vl = new BLL.V_CARSET().GetAllList();
            DropDownList1.DataSource = vl;
            DropDownList1.DataTextField = "MC";
            DropDownList1.DataValueField = "DM";
            DropDownList1.DataBind();
            ListItem li = new ListItem("", "-1");
            DropDownList1.Items.Insert(0, li);
          //  BindDataList();
            Init_Province();
        }

        Session["FCode"]="200500101";
    }

    private void BindDataList()
    {
        //StringBuilder tablename = new StringBuilder();
        //StringBuilder fields = new StringBuilder();

        //tablename.Append("[V_CUSTOMER] LEFT JOIN [V_CUSTOMER_AUTO] ON [V_CUSTOMER].[DM]=[V_CUSTOMER_AUTO].[VC_CODE] LEFT JOIN [V_VIPSET] ON [V_CUSTOMER].[DM]=[V_VIPSET].[GKDM]");
        //fields.Append("[V_CUSTOMER].*,[V_VIPSET].GKDM,CDM=[V_VIPSET].DM");
        
        string sqlstr = "";
        
        string FCode = Session["FCode"].ToString();
        if (!Check.CheckPermission(FCode, "00033"))
        {
            //#region
            //if (txtDM.Text.Trim() == "" && txtName.Text.Trim() == "" && txtNickName.Text.Trim() == "" && DropDownList1.SelectedValue == "-1" && txtCardCode.Text.Trim() == "" && txtDH.Text.Trim() == "" && txtPhone.Text.Trim() == "" && txtDZ.Text.Trim() == ""
            //    && txtQQ.Text.Trim() == "" && txtMSN.Text.Trim() == "" && txtWW.Text.Trim() == "" && txtBZ.Text.Trim() == ""&&txtEmail.Text.Trim()=="")
            //{
            //    sqlstr += " and V_Customer.DM='' ";
            //    sqlstr += " and V_CUSTOMER.GKMC='' ";
            //    sqlstr += " and V_CUSTOMER.BYZD5='' ";
            //    sqlstr += " and V_VIPSET.KLDM='' ";
            //    sqlstr += " and V_VIPSET.DM='' ";
            //    sqlstr += " and V_CUSTOMER.DH='' ";
            //    sqlstr += " and V_CUSTOMER.SJ='' ";
            //    sqlstr += " and V_CUSTOMER.DZ='' ";
            //    sqlstr += " and V_Customer_Auto.BYZD16='' ";
            //    sqlstr += " and V_Customer_Auto.BYZD17='' ";
            //    sqlstr += " and V_Customer_Auto.BYZD18='' ";
            //    sqlstr += " and V_CUSTOMER.BZ='' ";
            //    sqlstr += " and V_CUSTOMER.EMAIL='' ";
            //}
            //else
            //{
            sqlstr += txtDM.Text.Trim() == "" ? "" : " and V_Customer.DM='" + txtDM.Text.Trim() + "' ";
            sqlstr += txtName.Text.Trim() == "" ? "" : " and V_CUSTOMER.GKMC='" + txtName.Text.Trim() + "' ";
            sqlstr += txtNickName.Text.Trim() == "" ? "" : " and V_CUSTOMER.BYZD5='" + txtNickName.Text.Trim() + "' ";
            sqlstr += DropDownList1.SelectedValue == "-1" ? "" : " and V_VIPSET.KLDM='" + DropDownList1.SelectedValue + "' ";
            sqlstr += txtCardCode.Text.Trim() == "" ? "" : " and V_VIPSET.DM='" + txtCardCode.Text.Trim() + "' ";
            sqlstr += txtDH.Text.Trim() == "" ? "" : " and V_CUSTOMER.DH='" + txtDH.Text.Trim() + "' ";
            sqlstr += txtPhone.Text.Trim() == "" ? "" : " and V_Customer.SJ='" + txtPhone.Text.Trim() + "' ";
            sqlstr += txtDZ.Text.Trim() == "" ? "" : " and V_Customer.DZ='" + txtDZ.Text.Trim() + "' ";
            sqlstr += txtQQ.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD18='" + txtQQ.Text.Trim() + "' "; //QQ
            sqlstr += txtMSN.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD16='" + txtMSN.Text.Trim() + "' "; //MSN
            sqlstr += txtWW.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD17='" + txtWW.Text.Trim() + "' "; //旺旺
            sqlstr += txtBZ.Text.Trim() == "" ? "" : " and V_Customer.BZ='" + txtBZ.Text.Trim() + "' ";    //备注
            sqlstr += txtEmail.Text.Trim() == "" ? "" : " and V_Customer.EMAIL='" + txtEmail.Text.Trim() + "' "; //邮箱

            sqlstr += DDL_Province.SelectedIndex < 1 ? "" : "and V_Customer_Auto.VC_PROVINCE='" + DDL_Province.SelectedValue + "'";
            sqlstr += DDL_City.SelectedIndex < 1 ? "" : " and V_Customer_Auto.VC_CITY='" + DDL_City.SelectedValue + "'";
            sqlstr += DDL_Area.SelectedIndex < 1 ? "" : " and V_Customer_Auto.VC_AREA='" + DDL_Area.SelectedValue + "'";
            sqlstr += TextBox1.Text.Trim() == "" ? "" : " and V_Customer_Auto.CreatDatetime >'" + TextBox1.Text.Trim() + "'";
            sqlstr += TextBox2.Text.Trim() == "" ? "" : " and V_Customer_Auto.CreatDatetime<'" + TextBox2.Text.Trim() + " 23:59:59.999'";
            sqlstr += TextBox3.Text.Trim() == "" ? "" : " and V_Customer.Age>=" + TextBox3.Text.Trim();
            sqlstr += TextBox4.Text.Trim() == "" ? "" : " and V_Customer.Age<=" + TextBox4.Text.Trim();
            //}
            //#endregion
        }
        else
        {
            #region
            sqlstr += txtDM.Text.Trim() == "" ? "" : " and V_Customer.DM LIKE '%" + txtDM.Text.Trim() + "%' ";
            sqlstr += txtName.Text.Trim() == "" ? "" : " and V_CUSTOMER.GKMC LIKE '%" + txtName.Text.Trim() + "%' ";
            sqlstr += txtNickName.Text.Trim() == "" ? "" : " and V_CUSTOMER.BYZD5 LIKE '%" + txtNickName.Text.Trim() + "%' ";
            sqlstr += DropDownList1.SelectedValue == "-1" ? "" : " and V_VIPSET.KLDM LIKE '%" + DropDownList1.SelectedValue + "%' ";
            sqlstr += txtCardCode.Text.Trim() == "" ? "" : " and V_VIPSET.DM LIKE '%" + txtCardCode.Text.Trim() + "%' ";
            sqlstr += txtDH.Text.Trim() == "" ? "" : " and V_CUSTOMER.DH LIKE '%" + txtDH.Text.Trim() + "%' ";
            sqlstr += txtPhone.Text.Trim() == "" ? "" : " and V_Customer.SJ LIKE '%" + txtPhone.Text.Trim() + "%' ";
            sqlstr += txtDZ.Text.Trim() == "" ? "" : " and V_Customer.DZ LIKE '%" + txtDZ.Text.Trim() + "%' ";
            sqlstr += txtQQ.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD18 LIKE '%" + txtQQ.Text.Trim() + "%' ";
            sqlstr += txtMSN.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD16 LIKE '%" + txtMSN.Text.Trim() + "%' ";
            sqlstr += txtWW.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD17 LIKE '%" + txtWW.Text.Trim() + "%' ";
            sqlstr += txtBZ.Text.Trim() == "" ? "" : " and V_Customer.BZ LIKE '%" + txtBZ.Text.Trim() + "%' ";
            sqlstr += txtEmail.Text.Trim() == "" ? "" : " and V_Customer.EMAIL LIKE '%" + txtEmail.Text.Trim() + "%' ";

            sqlstr += DDL_Province.SelectedIndex < 1 ? "" : "and V_Customer_Auto.VC_PROVINCE='" + DDL_Province.SelectedValue + "'";
            sqlstr += DDL_City.SelectedIndex < 1 ? "" : " and V_Customer_Auto.VC_CITY='" + DDL_City.SelectedValue + "'";
            sqlstr += DDL_Area.SelectedIndex < 1 ? "" : " and V_Customer_Auto.VC_AREA='" + DDL_Area.SelectedValue + "'";
            sqlstr += TextBox1.Text.Trim() == "" ? "" : " and V_Customer_Auto.CreatDatetime >'" + TextBox1.Text.Trim() + "'";
            sqlstr += TextBox2.Text.Trim() == "" ? "" : " and V_Customer_Auto.CreatDatetime<'" + TextBox2.Text.Trim() + " 23:59:59.999'";
            sqlstr += TextBox3.Text.Trim() == "" ? "" : " and V_Customer.Age>=" + TextBox3.Text.Trim();
            sqlstr += TextBox4.Text.Trim() == "" ? "" : " and V_Customer.Age<=" + TextBox4.Text.Trim();
            #endregion
        }



        int recordCount,pagecount;
        //dlistCustomer.DataSource = new BLL.V_CUSTOMER().GetList2(tablename.ToString(), fields.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "[V_CUSTOMER].[DM]", false, sqlstr, out pagecount, out recordCount);
        dlistCustomer.DataSource = new BLL.V_CUSTOMER().GetCustomerInfoList(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, sqlstr, out recordCount);
        dlistCustomer.DataBind();
        AspNetPager1.RecordCount = recordCount;
    }
    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindDataList();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataList();
    }
    protected void Init_Province()
    {
        List<Model.Province> provinces = new BLL.Province().GetAllList();
        DDL_Province.DataSource = provinces;
        DDL_Province.DataTextField = "ProName";
        DDL_Province.DataValueField = "ProId";
        DDL_Province.DataBind();
        ListItem item = new ListItem();
        item.Value = "-1";
        item.Text = "请选择";
        DDL_Province.Items.Insert(0, item);

        DDL_City.Items.Add(new ListItem("请选择", "-1"));
        DDL_Area.Items.Add(new ListItem("请选择", "-1"));
    }
    protected void DDL_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL_Province.SelectedIndex > 0)
        {
            List<Model.City> citys = new List<Model.City>();
            BLL.City bll_city = new BLL.City();
            citys = bll_city.GetAllList(DDL_Province.SelectedValue);
            DDL_City.DataSource = citys;
            DDL_City.DataTextField = "CityName";
            DDL_City.DataValueField = "CityId";
            DDL_City.DataBind();

            ListItem item = new ListItem();
            item.Value = "-1";
            item.Text = "请选择";
            DDL_City.Items.Insert(0, item);
        }        
    }
    protected void DDL_City_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL_City.SelectedIndex > 0)
        {
            List<Model.Area> Areas = new List<Model.Area>();
            BLL.Area bll_area = new BLL.Area();
            Areas = bll_area.GetAllList(DDL_City.SelectedValue);
            DDL_Area.DataSource = Areas;
            DDL_Area.DataTextField = "AreaName";
            DDL_Area.DataValueField = "AreaId";
            DDL_Area.DataBind();

            ListItem item = new ListItem();
            item.Value = "-1";
            item.Text = "请选择";
            DDL_Area.Items.Insert(0, item);
        }
    }
    protected void DDL_Area_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
